const Service = require('node-windows').Service;
const path = require('path');

// 创建一个新的服务对象
const svc = new Service({
  name: 'LogServer',
  description: 'Log Server WebSocket Service',
  script: path.join(__dirname, 'server.js'),
  nodeOptions: [
    '--harmony',
    '--max_old_space_size=4096'
  ],
  env: [
    {
      name: "NODE_ENV",
      value: "production"
    },
    {
      name: "PORT",
      value: "3333"
    }
  ]
});

// 监听安装事件
svc.on('install', function() {
  console.log('Log Server service installed successfully!');
  console.log('Starting service...');
  svc.start();
});

// 监听启动事件
svc.on('start', function() {
  console.log('Log Server service started successfully!');
  console.log('Service is now running on port 3333');
});

// 监听错误事件
svc.on('error', function(err) {
  console.error('Service error:', err);
});

// 安装服务
console.log('Installing Log Server as Windows service...');
svc.install();